package cn.wolfcode.controller;

import cn.wolfcode.domain.Strategy;
import cn.wolfcode.domain.StrategyTheme;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.query.StrategyQueryObject;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.service.IStrategyCatalogService;
import cn.wolfcode.service.IStrategyService;
import cn.wolfcode.service.IStrategyThemeService;
import cn.wolfcode.util.JsonResult;
import cn.wolfcode.vo.CatalogVO;
import javafx.fxml.LoadException;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 攻略控制器类
 * @Author: 伍家钰
 * @Date: 2020/8/10 10:40
 * @Version: 1.0
 */
@Controller
@RequestMapping("strategy")
public class StrategyController {
    @Resource
    private IStrategyService strategyService;
    @Resource
    private IStrategyThemeService strategyThemeService;
    @Resource
    private IStrategyCatalogService strategyCatalogService;

    /**
     * 分页查询
     *
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") StrategyQueryObject qo) {
        Page<Strategy> page = strategyService.query(qo);
        model.addAttribute("page", page);
        return "strategy/list";
    }

    /**
     * 攻略的添加和编辑
     *
     * @return
     */
    @RequestMapping("input")
    public String input(Model model, String id) {
        // strategy,输入框数据的回显
        Strategy strategy = strategyService.get(id);
        model.addAttribute("strategy", strategy);

        /**
         * 查询出分类下拉列表的数据
         */
        List<CatalogVO> catalogs = strategyCatalogService.groupList();
        // catalogs:区域分类
        model.addAttribute("catalogs", catalogs);

        //themes:主题下拉列表的数据
        List<StrategyTheme> themes = strategyThemeService.list();
        model.addAttribute("themes", themes);
        return "strategy/input";
    }

    /**
     * 保存或修改
     *
     * @param strategy
     * @return
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy) {
        // 编辑或保存
        strategyService.saveOrUpdate(strategy);
        return JsonResult.success();
    }


    /**
     * 删除攻略
     *
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(String id) {
        strategyService.delete(id);
        return JsonResult.success();
    }
}
